<?php
/**
 * @author Jumper Swordwave
 * date: 2021/04/19
 */
declare(strict_types=1);
namespace App\Game\Core;

use App\Constants\ErrorCode;

/**
 * game response
 */
class Response
{
    /**
     * @var integer
     * 返回码
     */
    private $code;

    /**
     * @var string
     * 返回提示消息
     */
    private $message;

    /**
     * @var integer
     * 返回命令
     */
    private $command;

    /**
     * @var array
     * 返回数据内容
     */
    private $data;

    public function __construct(int $code, int $command, string $message, array $data = [])
    {
        $this->code = $code;
        $this->message = $message;
        $this->command = $command;
        $this->data = $data;
    }
    
    public static function newResponse(int $code, int $command, string $message, array $data = [])
    {
        return new static($code, $command, $message, $data);
    }

    public static function success(int $command, array $data = [])
    {
        return new static(ErrorCode::SUCCESS, $command, '', $data);
    }
    
    public static function error(int $code, string $message, int $command = 0, array $data = [])
    {
        return new static($code, $command, $message, $data);
    }

    public function getCommand()
    {
        return $this->command();
    }
    
    public function getCode()
    {
        return $this->code;
    }
    
    public function getMessage()
    {
        return $this->message;
    }
    
    public function getData()
    {
        return $data;
    }

    public function getResult()
    {
        return [
            'code' => $this->code,
            'command' => $this->command,
            'message' => $this->message,
            'data' => $this->data,
        ];
    }
}